//
//  SubTableView.m
//
//
//  Created by xu on 15/11/8.
//
//

#import "IndexThemeTableView.h"
#import "BaseViewController.h"
#import "IndexThemeCell.h"
#import "Util.h"

@implementation IndexThemeTableView
{
    UILabel *_loadText;
    UIActivityIndicatorView *_loadView;
    AFHTTPRequestOperationManager *_manager;
    BOOL _uploading;
    BOOL _isLastPage;
}

- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
    self = [super initWithFrame:frame style:style];
    if (self) {
        
        [self _createView];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self _createView];
    }
    return self;
}


- (void)_createView
{
    self.dataSource = self;
    self.delegate = self;
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 28)];
    _loadText = [[UILabel alloc] initWithFrame:CGRectMake(kScreenWidth/2-30, 0, 60, 28)];
    _loadText.font = [UIFont systemFontOfSize:15];
    _loadText.textColor = LightGray;
    _loadText.textAlignment = NSTextAlignmentCenter;
    _loadText.text=@"加载更多";
    _loadView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    _loadView.frame = CGRectMake(kScreenWidth/2-50, 12, 5, 5);
    [_loadView startAnimating];
    _loadView.tag = 200;
    [footerView addSubview:_loadText];
    [footerView addSubview:_loadView];
    footerView.backgroundColor = BackGray;
    self.tableFooterView = footerView;
}
- (void)setIsLastPage:(BOOL)isLastPage
{
    _isLastPage = isLastPage;
    if (!isLastPage) {
        _loadText.text=@"加载更多";
    }else
    {
        _loadText.text=@"加载完成";
    }
    [_loadView stopAnimating];
    _uploading = NO;
    
}

- (void)loadMoreAction
{
    if (_uploading == NO&&!_isLastPage) {
        _uploading = YES;
        _loadText.text=@"正在加载";
        [_loadView startAnimating];
        [self initLastData];
    }
}
//  父类中已经添加了tableview的代理
-(void)initData{
    _nilArray = [[NSMutableArray alloc] init];
//    _manager = [AFHTTPRequestOperationManager manager];
//    NSString *url=[NSString stringWithFormat:@"%@Records/GetIncomeDetail",BaseURL];
//    NSDictionary *parameters = @{@"memberid":@1011,@"lastid":@"0",@"pageSize":@"20",@"signature":[Util getSignature:[NSString stringWithFormat:@"%@020",Getmemberid]]};
//    AFHTTPRequestOperation*op= [_manager POST:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
//        NSError*error;
//        NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:&error];
//        NSArray *result = [dic valueForKey:@"Datas"];
       // if (error==nil&&![result isEqual:[NSNull null]]&&result!=nil&&result.count!=0) {
            for (int i = 0; i<3; i++) {
                //NSDictionary *data= result[i];
                 NSDictionary *parameters = @{@"memberid":@1011,@"lastid":@"上网而为",@"pageSize":@"啊啊啊啊啊啊啊",@"signature":[Util getSignature:[NSString stringWithFormat:@"%@020",Getmemberid]]};
                [_nilArray addObject:parameters];
            }
      //  }
        [self reloadData];
     //   [self doneLoadingTableViewData];
        if ([_nilArray count]<3) {
            [self setIsLastPage:YES];
        }else{
            [self setIsLastPage:NO];
        }
        
//    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        [[[UIAlertView alloc] initWithTitle: @"网络异常" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil] show];
//     //   [self doneLoadingTableViewData];
//    }];
//    op.responseSerializer = [AFCompoundResponseSerializer serializer];
//    [op start];
}

-(void)initLastData{
//    if (_nilArray.count>0) {
//        
//        
//        Income *m =[_nilArray objectAtIndex:[_nilArray count]-1];
//        _manager = [AFHTTPRequestOperationManager manager];
//        NSString *url=[NSString stringWithFormat:@"%@Records/GetIncomeDetail",BaseURL];
//        _lastid = [m IncomeId];
//        NSDictionary *parameters = @{@"memberid":@1011,@"lastid":_lastid,@"pageSize":@"10",@"signature":[Util getSignature:[NSString stringWithFormat:@"%@%@10",Getmemberid,_lastid]]};
//        AFHTTPRequestOperation*op= [_manager POST:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
//            NSError*error;
//            NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:&error];
//            NSArray *result = [dic valueForKey:@"Datas"];
            //NSLog(@"%@",dic);
           // if (error==nil&&![result isEqual:[NSNull null]]&&result!=nil&&result.count!=0) {
//                NSMutableArray *indexPathArray = [NSMutableArray array];
                for (int i = 0; i<3; i++) {
                    //NSDictionary *data= result[i];
                    NSDictionary *parameters = @{@"memberid":@1011,@"lastid":@"4324234234水电费",@"pageSize":@"啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊",@"signature":[Util getSignature:[NSString stringWithFormat:@"%@020",Getmemberid]]};
                    [_nilArray addObject:parameters];
                }
//                [self insertRowsAtIndexPaths:indexPathArray withRowAnimation:UITableViewRowAnimationLeft];
                if ([_nilArray count] >20) {
                    [self setIsLastPage:YES];
                }else{
                    [self setIsLastPage:NO];
                }
          //  }else{
          //      [self setIsLastPage:YES];
          //  }
            [self reloadData];
            
//        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//            [[[UIAlertView alloc] initWithTitle: @"网络异常" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil] show];
//        }];
//        op.responseSerializer = [AFCompoundResponseSerializer serializer];
//        [op start];
//    }
}
//实现TABLE方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _nilArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    IndexThemeCell *cell = [IndexThemeCell createWithTableView:tableView];
    if (_nilArray!=nil&&_nilArray.count>0) {
        NSDictionary *ic = [_nilArray objectAtIndex:indexPath.row];
        cell.title.text = ic[@"lastid"];
        cell.desc.text = ic[@"pageSize"];
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

@end
